"""
logging配置
"""

import os
import logging.config
import time

# 定义三种日志输出格式 开始


class CusLog(object):
    def __init__(self):

        standard_format = (
            "[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]"
            "[%(levelname)s][%(message)s]"
        )  # 其中name为getlogger指定的名字

        console_format = (
            "[%(asctime)s][task_id:%(name)s][%(filename)s:%(lineno)d]"
            "[%(levelname)s][%(message)s]"
        )  # 其中name为getlogger指定的名字

        # simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
        simple_format = "[%(asctime)s]%(message)s"
        sender_format = "from sender :  %(message)s"

        # 定义日志输出格式 结束
        # print(os.path.dirname(os.path.abspath(__file__)))
        logfile_dir = (
            os.path.dirname(os.path.abspath(__file__)) + "/" + "log"
        )  # log文件的目录

        if not os.path.exists(logfile_dir):
            os.makedirs(logfile_dir)

        # if not os.path.exists(logfile_send_dir):
        #     os.makedirs(logfile_send_dir)

        current_day = time.strftime("%Y-%m-%d", time.localtime())

        
        fix_svc_log_file_name = logfile_dir + "/" + "svc.log"

        app_file_name = logfile_dir + "/app.log"

        
        # print(logfile_path)
        app_file_path = os.path.join(logfile_dir, app_file_name)

        # print(logfile_send_path)

        # log配置字典
        LOGGING_DIC = {
            "version": 1,
            "disable_existing_loggers": True,
            "formatters": {
                "standard": {"format": standard_format},
                "simple": {"format": simple_format},
            },
            "filters": {},
            "handlers": {
                # 打印到终端的日志
                "console": {
                    "level": "INFO",
                    "class": "logging.StreamHandler",  # 打印到屏幕
                    "formatter": "standard",
                },
                # 打印到文件的日志,收集info及以上的日志
                "default": {
                    "level": "INFO",
                    "class": "logging.handlers.RotatingFileHandler",  # 保存到文件
                    "formatter": "standard",
                    "filename": fix_svc_log_file_name,  # 日志文件
                    "maxBytes": 1024 * 1024 * 1,  # 日志大小 5M
                    "backupCount": 20,
                    "encoding": "utf-8",  # 日志文件的编码，再也不用担心中文log乱码了
                },
                "timedRotating": {
                    "level": "INFO",
                    "class": "logging.handlers.TimedRotatingFileHandler",
                    "when": "midnight",
                    "interval": 1,
                    "filename": app_file_path,
                    "encoding": "utf-8",
                    "backupCount": 5,
                    "formatter": "simple",
                },
            },
            "loggers": {
                # logging.getLogger(__name__)拿到的logger配置
                "dev": {
                    "handlers": [
                        "timedRotating",
                        "default",
                        "console",
                    ],  # 这里把上面定义的两个handler都加上，即log数据既写入文件又打印到屏幕
                    "level": "INFO",
                    "propagate": True,  # 向上（更高level的logger）传递
                },
                "prod": {
                    "handlers": [
                        "default",
                        "console",
                    ],  # 这里把上面定义的两个handler都加上，即log数据既写入文件又打印到屏幕
                    "level": "INFO",
                    "propagate": True,  # 向上（更高level的logger）传递
                },
            },
        }

        logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置


if __name__ == "__main__":
    cuslog = CusLog()
    logger = logging.getLogger("dev")  # 生成一个log实例
    send_logger = logging.getLogger("prod")  # 生成一个log实例
    for i in range(20000):
        # logger.info("It works! {}".format(i))  # 记录该文件的运行状态
        send_logger.info("hhhhh --> {}".format(i))
